﻿using System;

namespace LogHelper.SQLiteORM.Attributes
{

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class ColumnAttribute : NamedAttribute
    {
        /// <summary>
        /// 指定数据库数据类型
        /// </summary>
        /// <param name="type">数据库数据类型</param>
        public ColumnAttribute(string type)
        {
            Type = type;
            IsPrimary = false;
            AllowNull = true;
        }

        public bool AllowNull { get; set; }

        public bool IsPrimary { get; set; }

//        public bool IsBool { get; set; }

        /// <summary>
        /// 指定Sqlite数据类型
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// 转换器，需要实现<see cref="IConvertor"/>接口 
        /// </summary>       
        public Type Convertor { get; set; }

        /// <summary>
        /// 字段的默认值(仅限于对新增列时对已有列数据的修改，新增数据通过代码实现默认值的设置)
        /// </summary>
        public object Default { get; set; }
    }

    public class ColumnDescriptor
    {
        /// <summary>
        /// 是否为主键
        /// </summary>
        public bool IsPrimary { get; internal set; }

        /// <summary>
        /// 字段名称
        /// </summary>
        public string Name { get; internal set; }

        /// <summary>
        /// 字段类型
        /// </summary>
        public string Type { get; internal set; }

        /// <summary>
        /// 默认值
        /// </summary>
        public object Default { get; set; }

        /// <summary>
        /// 是否允许为空
        /// </summary>
        public bool AllowNull { get; internal set; }

        /// <summary>
        /// 是否自增长
        /// </summary>
        public bool IsAutoIncrement { get; set; }
        
        public string GetNameOrDefault(string @default)
        {
            return !string.IsNullOrEmpty(Name) ? Name : @default;
        }
    }
}